package com.px.device.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.px.common.core.domain.entity.SysUser;
import com.px.common.exception.ServiceAppException;
import com.px.common.utils.DateUtils;
import com.px.common.utils.StringUtils;
import com.px.device.service.IDroneInfoService;
import com.px.system.domain.DroneInfo;
import com.px.system.domain.vo.AppVo;
import com.px.system.mapper.DroneInfoMapper;
import com.px.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 无人机信息Service业务层处理
 * 
 * @author
 * @date 2024-08
 */
@Service
public class DroneInfoServiceImpl implements IDroneInfoService 
{
    @Autowired
    private DroneInfoMapper droneInfoMapper;

    @Autowired
    private SysUserMapper userMapper;

    /**
     * 查询无人机信息
     * 
     * @param id 无人机信息主键
     * @return 无人机信息
     */
    @Override
    public DroneInfo selectDroneInfoById(Long id)
    {
        return droneInfoMapper.selectDroneInfoById(id);
    }

    /**
     * 查询无人机信息列表
     * 
     * @param droneInfo 无人机信息
     * @return 无人机信息
     */
    @Override
    public List<DroneInfo> selectDroneInfoList(DroneInfo droneInfo)
    {
        return droneInfoMapper.selectDroneInfoList(droneInfo);
    }

    /**
     * 新增无人机信息
     * 
     * @param droneInfo 无人机信息
     * @return 结果
     */
    @Override
    public int insertDroneInfo(DroneInfo droneInfo)
    {
        return droneInfoMapper.insertDroneInfo(droneInfo);
    }

    /**
     * 修改无人机信息
     * 
     * @param droneInfo 无人机信息
     * @return 结果
     */
    @Override
    public int updateDroneInfo(DroneInfo droneInfo)
    {
        return droneInfoMapper.updateDroneInfo(droneInfo);
    }

    /**
     * 批量删除无人机信息
     * 
     * @param ids 需要删除的无人机信息主键
     * @return 结果
     */
    @Override
    public int deleteDroneInfoByIds(Long[] ids)
    {
        return droneInfoMapper.deleteDroneInfoByIds(ids);
    }

    /**
     * 删除无人机信息信息
     * 
     * @param id 无人机信息主键
     * @return 结果
     */
    @Override
    public int deleteDroneInfoById(Long id)
    {
        return droneInfoMapper.deleteDroneInfoById(id);
    }

    @Override
    public DroneInfo selectDroneInfoBySn(String sn) {
        DroneInfo droneInfo = new DroneInfo();
        droneInfo.setSn(sn);
        List<DroneInfo> droneInfos = selectDroneInfoList(droneInfo);
        if (CollectionUtil.isEmpty(droneInfos)) {
            return null;
        }
        return droneInfos.get(0);
    }


    @Override
    public void online(AppVo appVo) {
        if (StringUtils.isEmpty(appVo.getSn()) || StringUtils.isNull(appVo.getState()))
        {
            throw new ServiceAppException("数据为空");
        }
        DroneInfo droneInfo = new DroneInfo();
        droneInfo.setSn(appVo.getSn());
        droneInfo.setDroneState(appVo.getState());
        droneInfoMapper.updateDroneStateInfoBySn(droneInfo);
    }

    @Override
    public int registerOrUpdate(DroneInfo droneInfo) {
        if (StringUtils.isEmpty(droneInfo.getSn()))
        {
            throw new ServiceAppException("设备SN码为空");
        }
        int i = 0;
        SysUser sysUser = userMapper.selectUserByUserName(droneInfo.getUsername());
        droneInfo.setDutyCode(sysUser.getUserName());
        droneInfo.setDutyName(sysUser.getNickName());
        //删除用户账户名称
        droneInfoMapper.updateDroneInfoByUserName(sysUser.getUserName());
        int count = droneInfoMapper.selectDroneInfoBySn(droneInfo.getSn());
        if(count > 0){
            i =droneInfoMapper.updateDroneInfoBySn(droneInfo);
        }else {
            droneInfo.setName(sysUser.getNickName()+"的设备");
            droneInfo.setCreateTime(DateUtils.getNowDate());
            i = droneInfoMapper.insertDroneInfo(droneInfo);
        }
        //电池页面

        return i;
    }

    @Override
    public List<DroneInfo> selectDroneInfoByUserNames(List<String> collect) {
        return droneInfoMapper.selectDroneInfoByUserNames(collect);
    }

}
